package com.snopy.designPattern.structure.flyweight;

import com.snopy.designPattern.structure.flyweight.impl.MidgetCar;
import com.snopy.designPattern.structure.flyweight.impl.SprintCar;
import com.snopy.designPattern.structure.flyweight.inter.RaceCar;

import java.util.HashMap;
import java.util.Map;

/**
 * @author snopy
 * @version 1.0.0
 * @classname RaceCarFactory
 * @description
 * @email 77912204@qq.com
 * @date 2022/03/03 16:30
 */
public class RaceCarFactory {
    private static final Map<String,RaceCar> map = new HashMap<>();
    public static RaceCar getRaceCar(String key){
        if (map.containsKey(key)){
            return map.get(key);
        }
        RaceCar raceCar = null;
        switch (key){
            case "Midget":
                raceCar = new MidgetCar();
                raceCar.name="Midget Car";
                raceCar.speed=140;
                raceCar.horsePower=400;
                break;
            case "Sprint":
                raceCar = new SprintCar();
                raceCar.name="Sprint Car";
                raceCar.speed=160;
                raceCar.horsePower=1000;
                break;
            default:
                throw new IllegalArgumentException("Unsupported car type.");
        }
        map.put(key,raceCar);
        return raceCar;
    }
}
